package io.lkt.task.bus.service.impl;

import io.lkt.task.bus.mapper.PunchClockArticleInfoMapper;
import io.lkt.task.bus.mapper.UserPunchClockInfoMapper;
import io.lkt.task.bus.model.PunchClockArticleInfo;
import io.lkt.task.bus.service.IUserPunchClockInfoService;
import io.lkt.task.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
public class UserPunchClockInfoServiceImpl implements IUserPunchClockInfoService {

    @Autowired
    private UserPunchClockInfoMapper userPunchClockInfoMapper;

    @Autowired
    private PunchClockArticleInfoMapper punchClockArticleInfoMapper;

    @Override
    public int getUserClockDays(Integer userId) {

        //查询累计的打卡天数
        int clockTimes = Optional.ofNullable(userPunchClockInfoMapper.queryUserPunchClockDays(userId)).orElse(0);

        return clockTimes;
    }

    @Override
    public PunchClockArticleInfo getUserPunchClockArticleInfo() {
        //true-上午 false-下午
        boolean resultStatus = ToolUtil.getAmPmTimePeriodStatus();

        int clockArticleTimeType = 1;
        if (!resultStatus) {
            clockArticleTimeType = 2;
        }

        PunchClockArticleInfo punchClockArticleInfo=punchClockArticleInfoMapper.queryCurDayPunchClockArticleInfo(clockArticleTimeType);
        if(punchClockArticleInfo==null){//如果为空就随机获取一条
            punchClockArticleInfo= punchClockArticleInfoMapper.queryRANDPunchClockArticleInfo(clockArticleTimeType);
        }
        return punchClockArticleInfo;
    }

    @Override
    public PunchClockArticleInfo getPunchClockArticleInfoById(Integer clockArticleId) {
        return punchClockArticleInfoMapper.queryPunchClockArticleInfoById(clockArticleId);
    }

    @Override
    public Integer getTotalUserPunchClockDays() {
        return userPunchClockInfoMapper.queryTotalUserPunchClockDays();
    }
}
